using System;
using Microsoft.Practices.Unity;

namespace UniAspect.UnityCommon
{
    /// <summary>
    /// Some nice methods that make unity prettier
    /// </summary>
    public static class ContainerExtensions
    {
        /// <summary>
        /// Registers a type that is created only once.
        /// (Recommended to use this over registering an instance)
        /// </summary>
        public static IUnityContainer RegisterSingleton<TInterface, TImplementation>(
                this IUnityContainer container) where TImplementation : TInterface
        {
            return container.RegisterType<TInterface, TImplementation>(
                    new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer RegisterSingleton<TInterface, TImplementation>(
                this IUnityContainer container, string name) where TImplementation : TInterface
        {
            return container.RegisterType<TInterface, TImplementation>(
                    name, new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer RegisterSingleton<T>(this IUnityContainer container)
        {
            return container.RegisterType<T>(new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer RegisterSingleton<T>(this IUnityContainer container, string name)
        {
            return container.RegisterType<T>(name, new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer RegisterSingleton(this IUnityContainer container, Type type)
        {
            return container.RegisterType(type, new ContainerControlledLifetimeManager());
        }

        public static IUnityContainer RegisterSingleton(this IUnityContainer container, Type from, Type to)
        {
            return container.RegisterType(from, to, new ContainerControlledLifetimeManager());
        }
        /// <summary>
        /// Registers type to be created every time!
        /// </summary>
        public static IUnityContainer RegisterTransientType<T>(this IUnityContainer container)
        {
            return container.RegisterType<T>(new TransientLifetimeManager());
        }

        public static IUnityContainer RegisterTransientType(this IUnityContainer container, Type t)
        {
            return container.RegisterType(t, new TransientLifetimeManager());
        }
    }
}